package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.model.CompanyAuditing;
import com.yami.shop.bean.model.ShopCompany;
import com.yami.shop.bean.param.CompanyInfoAuditParam;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.CompanyAuditingMapper;
import com.yami.shop.service.CompanyAuditingService;
import com.yami.shop.service.ShopCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author chiley
 * @date 2022/9/21 15:00
 */
@Service
public class CompanyAuditingServiceImpl extends ServiceImpl<CompanyAuditingMapper, CompanyAuditing> implements CompanyAuditingService {

    @Autowired
    private ShopCompanyService shopCompanyService;

    @Autowired
    private CompanyAuditingMapper companyAuditingMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyChangeCompanyInfo(ShopCompany shopCompany, String userId) {
        // 保存变更工商信息
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        shopCompanyService.saveInfo(shopCompany);
        // 生成审核信息
        generateAuditInformation(shopCompany, userId);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(CompanyAuditing companyAuditing) {

        if (!Objects.equals(companyAuditing.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new YamiShopBindException("yami.Auditing.applicationInfo.notFound");
        }
        // 删除申请变更的工商信息
        shopCompanyService.remove(new LambdaQueryWrapper<ShopCompany>().eq(ShopCompany::getShopCompanyId, companyAuditing.getShopCompanyId()));
        // 删除申请审核信息
        removeById(companyAuditing.getCompanyAuditingId());
    }


    @Override
    public IPage<CompanyAuditing> page(Page<CompanyAuditing> page, CompanyInfoAuditParam companyInfoAuditParam) {
        return companyAuditingMapper.page(page, companyInfoAuditParam);
    }

    @Override
    public void audit(CompanyAuditing companyAuditing) {
        CompanyAuditing companyAuditingDb = companyAuditingMapper.selectById(companyAuditing.getCompanyAuditingId());
        if (Objects.isNull(companyAuditingDb) || !Objects.equals(companyAuditingDb.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new YamiShopBindException("yami.Auditing.applicationInfo.notFound");
        }
        // 审核不通过
        if (Objects.equals(companyAuditing.getStatus(), AuditStatus.FAILAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.FAILAUDIT.value());
            companyAuditing.setRemarks(companyAuditing.getRemarks());
            companyAuditingMapper.updateById(companyAuditing);
            // 把对应的工商信息设置为审核未通过状态
            shopCompanyService.update(new LambdaUpdateWrapper<ShopCompany>().set(ShopCompany::getStatus, AuditStatus.FAILAUDIT.value()).eq(ShopCompany::getShopCompanyId, companyAuditing.getShopCompanyId()));
            return;
        }
        // 审核通过(if不能省略，防止传入非法参数)
        if (Objects.equals(companyAuditing.getStatus(), AuditStatus.SUCCESSAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.SUCCESSAUDIT.value());
            companyAuditingMapper.updateById(companyAuditing);
            // 把之前审核不通过的工商信息删除
            shopCompanyService.remove(new LambdaQueryWrapper<ShopCompany>().eq(ShopCompany::getShopId, companyAuditing.getShopId()).ne(ShopCompany::getShopCompanyId, companyAuditingDb.getShopCompanyId()));
            // 把对应的工商信息设置为启用状态
            shopCompanyService.update(new LambdaUpdateWrapper<ShopCompany>().set(ShopCompany::getStatus, AuditStatus.SUCCESSAUDIT.value()).eq(ShopCompany::getShopCompanyId, companyAuditingDb.getShopCompanyId()));

        }
    }

    @Override
    public CompanyAuditing getAuditInfo(Long shopId) {
        CompanyAuditing latestCompanyAuditing = getOne(new LambdaQueryWrapper<CompanyAuditing>().eq(CompanyAuditing::getShopId, shopId).orderByDesc(CompanyAuditing::getCreateTime), false);
        if (Objects.isNull(latestCompanyAuditing)) {
            return new CompanyAuditing();
        }
        latestCompanyAuditing.setShopCompany(shopCompanyService.getById(latestCompanyAuditing.getShopCompanyId()));
        return latestCompanyAuditing;
    }

    /**
     * 生成店铺申请记录
     *
     * @param shopCompany
     * @param userId
     */
    private void generateAuditInformation(ShopCompany shopCompany, String userId) {
        CompanyAuditing companyAuditing = new CompanyAuditing();
        companyAuditing.setShopId(shopCompany.getShopId());
        companyAuditing.setShopCompanyId(shopCompany.getShopCompanyId());
        companyAuditing.setUserId(userId);
        companyAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        save(companyAuditing);
    }
}
